package com.hn.gege.sys.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.gege.sys.modules.sys.dto.JurisdictionTreeDTO;
import com.hn.gege.sys.modules.sys.entity.Jurisdiction;
import com.hn.gege.sys.modules.sys.mapper.JurisdictionMapper;
import com.hn.gege.sys.modules.sys.service.IJurisdictionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限资源表 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2020-08-06
 */
@Service
public class JurisdictionServiceImpl extends ServiceImpl<JurisdictionMapper, Jurisdiction> implements IJurisdictionService {

    @Override
    public Jurisdiction getByPk(String id) {
        LambdaQueryWrapper<Jurisdiction> lambda = new QueryWrapper<Jurisdiction>().lambda()
                .eq(Jurisdiction::getId, id);
        return baseMapper.selectOne(lambda);
    }

    @Override
    public boolean updateByPk(Jurisdiction jurisdiction) {
        LambdaQueryWrapper<Jurisdiction> lambda = new QueryWrapper<Jurisdiction>().lambda()
                .eq(Jurisdiction::getId, jurisdiction.getId());
        return super.update(jurisdiction, lambda);
    }

    @Override
    public boolean removeByPk(String id) {
        LambdaQueryWrapper<Jurisdiction> lambda = new QueryWrapper<Jurisdiction>().lambda()
                .eq(Jurisdiction::getId, id);
        return super.remove(lambda);
    }

    @Override
    public List<JurisdictionTreeDTO> tree() {
        List<JurisdictionTreeDTO> jurisdictionTreeDTOS = baseMapper.selectTreeByParentId("0");
        return treeAll(jurisdictionTreeDTOS);
    }

    @Override
    public boolean removeChild(String parentId) {
        return false;
    }


    /**
     * 生成树形数据
     * @param list
     * @return
     */
    private List<JurisdictionTreeDTO> treeAll(List<JurisdictionTreeDTO> list){
        if (list == null || list.size() == 0)
            return null;

        list.forEach(item -> {
            List<JurisdictionTreeDTO> jurisdictionTreeDTOS = baseMapper.selectTreeByParentId(item.getId());
            item.setChildren(treeAll(jurisdictionTreeDTOS));
        });
        return list;
    }

}
